package com.kuzlak.cms.module.sys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuzlak.cms.bean.MyPage;
import com.kuzlak.cms.handler.MyLambdaQueryWrapper;
import com.kuzlak.cms.module.sys.entity.SysDict;
import com.kuzlak.cms.module.sys.mapper.SysDictMapper;
import com.kuzlak.cms.module.sys.service.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* @author hesenjan
* @description 针对表【sys_dict】的数据库操作Service实现
* @createDate 2024-11-30 23:46:05
*/
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict>
    implements SysDictService{

    @Override
    public boolean deleteById(long id) {

        int i = this.baseMapper.deleteById(id);
        return (i != 0);
    }

    @Override
    public MyPage findByPage(int pagesize, int page, String name, int status, String isKey, String startDate, String endDate, String remark) {
        MyLambdaQueryWrapper<SysDict> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SysDict::getId).as("id")
                .select(SysDict::getName).as("name")
                .select(SysDict::getIsKey).as("isKey")
                .select(SysDict::getRemark).as("remark")
                .select(SysDict::getStatus).as("status")
                .select(SysDict::getDate).as("date");

        queryWrapper.orderByDesc(SysDict::getId);
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.likeRight(SysDict::getName, name);
        }

        if (StringUtils.isNotEmpty(remark)) {
            queryWrapper.likeRight(SysDict::getRemark, remark);
        }
        if (StringUtils.isNotEmpty(startDate)) {
            queryWrapper.apply(StringUtils.isNotBlank(startDate), "DATE(DATE)>=DATE_FORMAT('" + startDate + "','%Y-%m-%d')");
        }

        if (StringUtils.isNotEmpty(endDate)) {
            queryWrapper.apply(StringUtils.isNotBlank(endDate), "DATE(DATE)<=DATE_FORMAT('" + endDate + "','%Y-%m-%d')");
        }
        if (StringUtils.isNotEmpty(isKey)) {
            queryWrapper.likeRight(SysDict::getIsKey, isKey);
        }
        if (status > 0) {
            queryWrapper.eq(SysDict::getStatus, status);
        }

        Page<Map<String, Object>> page0 = new Page<>(page, pagesize);
        IPage<Map<String, Object>> data = this.baseMapper.selectMapsPage(page0, queryWrapper);

        System.out.println(data.getTotal());
        return MyPage.parsePage(data);
    }
}


